/**
 * 
 */
package org.log4jconfig.xml.editor.contentassist;

import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor;
import org.log4jconfig.xml.editor.Log4jConfigXmlEditorIntegrationPlugin;
import org.log4jconfig.xml.editor.contentassist.attribute.AppenderClassContentAssistComputer;
import org.log4jconfig.xml.editor.contentassist.attribute.AppenderRefRefComputer;
import org.log4jconfig.xml.editor.contentassist.attribute.LayoutClassContentAssistComputer;
import org.log4jconfig.xml.editor.contentassist.attribute.LevelOrPriorityValueContentAssistComputer;
import org.log4jconfig.xml.editor.contentassist.attribute.LoggerOrCategoryNameContentAssistComputer;
import org.log4jconfig.xml.editor.contentassist.attribute.ParamNameContentAssistComputer;
import org.log4jconfig.xml.editor.utils.ContentAssistRequestUtils;
import org.log4jconfig.xml.editor.utils.JdtUtils;
import org.log4jconfig.xml.editor.utils.Logger;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class AttributeValueContentAssistProcessor extends XMLContentAssistProcessor {

	/**
	 * 
	 */
	private final LoggerOrCategoryNameContentAssistComputer loggerOrCategoryNameContentAssistComputer = new LoggerOrCategoryNameContentAssistComputer();

	/**
	 * 
	 */
	private final IAttributeContentAssistComputer levelOrPriorityValueContentAssistProcessor = new LevelOrPriorityValueContentAssistComputer();

	/**
	 * 
	 */
	private final IAttributeContentAssistComputer appenderClassContentAssistProcessor = new AppenderClassContentAssistComputer();

	/**
	 * 
	 */
	private final IAttributeContentAssistComputer appenderRefRefContentAssistProcessor = new AppenderRefRefComputer();

	/**
	 * 
	 */
	private final IAttributeContentAssistComputer layoutClassContentAssistProcessor = new LayoutClassContentAssistComputer();

	/**
	 * 
	 */
	private final IAttributeContentAssistComputer paramNameContentAssistProcessor = new ParamNameContentAssistComputer();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor
	 * #addAttributeValueProposals
	 * (org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest)
	 */
	@Override
	protected void addAttributeValueProposals(final ContentAssistRequest contentAssistRequest) {
		final String elementName = contentAssistRequest.getNode().getNodeName();
		final String attributeName = ContentAssistRequestUtils.extractAttributeName(contentAssistRequest);
		final String attributeMatchValue = ContentAssistRequestUtils.extractAttributeMatchValue(contentAssistRequest);
		final String attributeCurrentValue = ContentAssistRequestUtils.extractAttributeCurrentValue(contentAssistRequest);
		try {
			final IAttributeContentAssistComputer contentAssistProcessor = getContentAssistProcessor(elementName,
					attributeName);

			if (contentAssistProcessor != null) {
				final IJavaProject javaProject = JdtUtils.getJavaProject(contentAssistRequest.getDocumentRegion()
						.getParentDocument());
				// use Platform Progress Service
				// FIXME : use new ProgressMonitorUtils class
				contentAssistProcessor.addAttributeValueProposals(javaProject, contentAssistRequest,
						attributeCurrentValue, attributeMatchValue);
			}
		} catch (Throwable e) {
			Logger.error("Error while computing content assist for element " + elementName + ": " + e.getMessage(), e);
		}
		super.addAttributeValueProposals(contentAssistRequest);
	}

	/**
	 * @param elementName
	 * @param attributeName
	 * @return
	 */
	private IAttributeContentAssistComputer getContentAssistProcessor(String elementName, String attributeName) {
		if ("logger".equalsIgnoreCase(elementName) && "name".equalsIgnoreCase(attributeName)) {
			return loggerOrCategoryNameContentAssistComputer;
		} else if ("category".equalsIgnoreCase(elementName) && "name".equalsIgnoreCase(attributeName)) {
			return loggerOrCategoryNameContentAssistComputer;
		} else if ("level".equalsIgnoreCase(elementName) && "value".equalsIgnoreCase(attributeName)) {
			return levelOrPriorityValueContentAssistProcessor;
		} else if ("priority".equalsIgnoreCase(elementName) && "value".equalsIgnoreCase(attributeName)) {
			return levelOrPriorityValueContentAssistProcessor;
		} else if ("appender".equalsIgnoreCase(elementName) && "class".equalsIgnoreCase(attributeName)) {
			return appenderClassContentAssistProcessor;
		} else if ("appender-ref".equalsIgnoreCase(elementName) && "ref".equalsIgnoreCase(attributeName)) {
			return appenderRefRefContentAssistProcessor;
		} else if ("layout".equalsIgnoreCase(elementName) && "class".equalsIgnoreCase(attributeName)) {
			return layoutClassContentAssistProcessor;
		} else if ("param".equalsIgnoreCase(elementName) && "name".equalsIgnoreCase(attributeName)) {
			return paramNameContentAssistProcessor;
		}
		Log4jConfigXmlEditorIntegrationPlugin.getDefault().getLog().log(
				new Status(Status.WARNING, Log4jConfigXmlEditorIntegrationPlugin.PLUGIN_ID,
						"Element name not (yet) supported for completion proposal: "
								+ elementName.equalsIgnoreCase(elementName)));
		return null;
	}

}
